﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SmartMD.DataModel.Entity;
using System.ComponentModel;
using SmartMD.Manager.Report;
using SmartMD.Manager.Specimens.Query;

namespace SmartMD.Manager.Specimens
{
    /// <summary>
    /// Interaction logic for QuerySpecListPad.xaml
    /// </summary>
    public partial class QuerySpecListPad : UserControl
    {
        public List<SpecQueryResult> printList = new List<SpecQueryResult>(); //打印列表
        public List<SpecQueryResult> printListView = new List<SpecQueryResult>(); //打印列表
        private ObservableCollection<SpecQueryResult> _specList;
        public ObservableCollection<SpecQueryResult> SpecList
        {
            get
            {
                return _specList;
            }
            set
            {
                if (value != null)
                {
                    _specList = value;
                    for (int i = 0; i < _specList.Count; i++)
                    {
                        printList.Add(_specList[i]);
                    }
                    ICollectionView view = CollectionViewSource.GetDefaultView(value);

                    SpecListView.ItemsSource = view;
                }               
            }
        }

        public QuerySpecListPad()
        {
            InitializeComponent();   
        }

        /**
         * 功能：清空打印列表
         * 
         * */
        public void Clear()
        {
            this.printList.Clear();
        }

        private void SpecList_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            object item = GetElementFromPoint((ItemsControl)sender, e.GetPosition((ItemsControl)sender));
            
            bool isItemSelected = (item != null);
            if (isItemSelected)
            {
                ContextMenu ctxMenu = new ContextMenu();
                MenuItem mItem = new MenuItem();
                mItem.Header = "添加";
                mItem.Click += this.AddPrint;
                mItem.Height = 25;
                mItem.Width = 160;
                ctxMenu.Items.Add(mItem);
                mItem = new MenuItem();
                mItem.Header = "取消";
                mItem.Click += this.RemovePrint;
                mItem.Height = 25;
                mItem.Width = 160;
                ctxMenu.Items.Add(mItem);
                mItem = new MenuItem();
                mItem.Height = 25;
                mItem.Header = "定位标本";
                mItem.Click += this.LocateSpec;
                ctxMenu.Items.Add(mItem);
                this.SpecListView.ContextMenu = ctxMenu;
            }
            else
            {
                this.SpecListView.ContextMenu = null;
            }
        }
        /**
         * 功能：添加打印
         * 
         * */
        private void AddPrint(object sender, RoutedEventArgs e)
        {
            UIElement item = ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem));
            SpecQueryResult seletedItem = (SpecQueryResult)(item as ListView).SelectedItem;
            seletedItem.State = DataModel.RecordState.Printed;
            if (printList.IndexOf(seletedItem) < 0)
            {
                printList.Add(seletedItem);
            }

            //添加列表打印------黄俊修改
            if (printListView.IndexOf(seletedItem) < 0)
            {
                printListView.Add(seletedItem);
            }
        }

        /**
       * 功能：取消打印
       * 
       * */
        private void RemovePrint(object sender, RoutedEventArgs e)
        {
            UIElement item = ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem));
            SpecQueryResult seletedItem = (SpecQueryResult)(item as ListView).SelectedItem;
            seletedItem.State = DataModel.RecordState.Normal;
            if (printList.IndexOf(seletedItem) >= 0)
            {
                printList.Remove(seletedItem);
            }

            //取消列表打印------黄俊修改
            if (printListView.IndexOf(seletedItem) >= 0)
            {
                printListView.Remove(seletedItem);
            }
        }    

      //  /**
      //* 功能：添加列表打印------黄俊修改
      //* 
      //* */
      //  private void AddListPrint(object sender, RoutedEventArgs e)
      //  {
      //      UIElement item = ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem));
      //      SpecQueryResult seletedItem = (SpecQueryResult)(item as ListView).SelectedItem;
      //      seletedItem.State = DataModel.RecordState.PrintedList;

      //      if (printListView.IndexOf(seletedItem) < 0)
      //      {
      //          printListView.Add(seletedItem);
      //      }
      //  }

      //  /**
      //   * 功能：取消列表打印------黄俊修改
      //   * 
      //  * */
      //  private void RemoveListPrint(object sender, RoutedEventArgs e)
      //  {
      //      UIElement item = ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem));
      //      SpecQueryResult seletedItem = (SpecQueryResult)(item as ListView).SelectedItem;
      //      seletedItem.State = DataModel.RecordState.Normal;
      //      if (printListView.IndexOf(seletedItem) >= 0)
      //      {
      //          printListView.Remove(seletedItem);
      //      }
      //  }        

        /**
         * 功能 : 定位标本
         * 
         * */
        private void LocateSpec(object sender, RoutedEventArgs e)
        {
            UIElement item = ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem));
            SpecQueryResult seletedItem = (SpecQueryResult)(item as ListView).SelectedItem;
            //LocateSpecDialog specLocateDlg = new LocateSpecDialog(seletedItem);
            //specLocateDlg.ShowDialog();       
        }

        private object GetElementFromPoint(ItemsControl itemsControl, Point point)
        {
            UIElement element = itemsControl.InputHitTest(point) as UIElement;
            while (element != null)
            {
                if (element == itemsControl)
                    return null;
                object item = itemsControl.ItemContainerGenerator.ItemFromContainer(element);
                if (!item.Equals(DependencyProperty.UnsetValue))
                    return item;
                element = (UIElement)VisualTreeHelper.GetParent(element);
            }
            return null;
        }
    }
}
